package com.gaohe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.common.BaseContext;
import com.gaohe.mapper.ClassesMapper;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.User;
import com.gaohe.pojo.UserClass;
import com.gaohe.service.ClassesService;
import com.gaohe.service.UserClassService;
import com.gaohe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {
    @Autowired
    private UserClassService userClassService;
    @Autowired
    private UserService userService;
    @Override
    public List<User> findByClasstoUsers(Long id) {
        LambdaQueryWrapper<UserClass> qw = new LambdaQueryWrapper<>();
        qw.eq(UserClass::getClassId,id);
        List<UserClass> list = userClassService.list(qw);
        ArrayList<Long> ids = new ArrayList<>();
        for (UserClass userClass : list) {
            ids.add(userClass.getUserId());
        }
        List<User> users = userService.listByIds(ids);
        return users;
    }
    @Override
    public List<String> getClassByCardTags(String tags) {
        String[] ctags = tags.split(",");
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.select(Classes::getName);
        for (String ctag : ctags) {
            qw.like(Classes::getTags, ctag).or();
        }
        List<String> classNames = this.listObjs(qw);
        return classNames;
    }

    @Override
    public List<Classes> getClassByTag(String tag) {
        //1.获取用户id
        Long userId = BaseContext.getCurrentId();
        //2.userid->userClass表
        LambdaQueryWrapper<UserClass> qw1 = new LambdaQueryWrapper<>();
        qw1.select(UserClass::getClassId);
        qw1.eq(UserClass::getUserId,userId);
        List<Long> userclassIds = userClassService.listObjs(qw1);
        //查询为选过的课程
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(Classes::getTags,tag);
        qw.notIn(StringUtils.isEmpty(userclassIds)&&userclassIds.size()!=0,Classes::getId,userclassIds);
        List<Classes> list = this.list(qw);
        return list;
    }


}
